package com.atguigu.gmall.cart.service;

import com.atguigu.gmall.cart.mapper.CartMapper;
import com.atguigu.gmall.cart.pojo.Cart;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class CartAsyncService {

    @Autowired
    private CartMapper cartMapper;

    //新增数据到数据库
    @Async
    public void insertMysql(String userId,Cart cart) {
        cartMapper.insert(cart);
    }

    //更新数据库
    @Async
    public void updateMysql(String userId,Cart cart,  String skuId) {
        //int i = 1/0;
        cartMapper.update(cart,new UpdateWrapper<Cart>().eq("user_id",userId).eq("sku_id",skuId));
    }

    public void deleteMysql(String userId) {
        cartMapper.delete(new UpdateWrapper<Cart>().eq("user_id",userId));
    }

    public void deleteMysqlByUserIdAndSkuId(String userId, String skuId) {
        cartMapper.delete(new UpdateWrapper<Cart>().eq("user_id",userId).eq("sku_id",skuId));
    }
}
